package com.woniu.atms.mapper;

import com.woniu.atms.entity.G5Site;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author LHY
 * @title SiteMapper
 * @createTime 2022/1/18/14:07
 **/
public interface SiteMapper {

    /**
     * 查询所有站点
     * @return
     */
    @Select("SELECT * FROM g5_site WHERE is_exit='y'")
    public List<G5Site> findAllSite();

    /**
     * 按id查询站点
     *
     * @param id
     * @return
     */
    @Select("SELECT * FROM g5_site WHERE id=#{id} and is_exit='y'")
    public G5Site findSiteById(int id);

    /**
     * 根据名称模糊查询
     *
     * @param name
     * @return
     */
    @Select("SELECT * FROM g5_site WHERE NAME LIKE CONCAT('%',#{name},'%') and is_exit='y'")
    public List<G5Site> findSiteByFuzzyName(String name);

    /**
     * 根据站点所属地区名模糊查询
     *
     * @return
     */
    @Select("SELECT * FROM g5_site WHERE address LIKE CONCAT('%',#{address},'%') and is_exit='y'")
    public List<G5Site> findSiteByFuzzyAddress(String address);

    /**
     * 添加站点
     * @param site
     * @return
     */
    @Insert("INSERT INTO g5_site(name,status,address,is_exit)" +
            "VALUES(#{name},#{status},#{address},#{isExit})")
    public boolean addSite(G5Site site);

    /**
     * 根据站点名和地区同时进行模糊查询
     * @param keyStr 匹配关键字
     * @return 返回查询列表结果
     */
    @Select("SELECT * FROM g5_site WHERE (name LIKE CONCAT('%',#{keyStr},'%') OR address LIKE CONCAT('%',#{keyStr},'%')) AND status='y' AND is_exit='y'")
    public List<G5Site> findSiteByFuzzyNameOrAddress(String keyStr);

    /**
     * 删除站点，
     * @param id
     * @return
     */
    @Update("UPDATE g5_site SET is_exit='n' WHERE id=#{id}")
    public boolean deleteSiteById(int id);

    /**
     * 更新站点信息
     *
     * @param site
     * @return
     */
    @UpdateProvider(value = SiteSql.class, method = "updateSite")
    public boolean updateSite(G5Site site);

    /**
     * 按站点名查找
     * @param name
     * @return
     */
    @Select("SELECT * FROM g5_site WHERE name=#{name} and is_exit='y'")
    public G5Site findSiteByName(String name);

    /**
     * 查询所有路线名
     * @return
     */
    @Select("SELECT name FROM g5_site")
    public List<String> findAllSiteNames();


    //动态sql
    public static class SiteSql {
        //动态更新
        public String updateSite(G5Site site) {
            SQL sql = new SQL();
            sql.UPDATE("g5_site");
            if (site.getName() != null && site.getName().trim().length() != 0 ) {
                sql.SET("name=#{name}");
            }
            if ( site.getAddress()!=null && site.getAddress().trim().length() != 0 ){
                sql.SET("address=#{address}");
            }
            if(site.getStatus()!=null && site.getStatus().trim().length()!=0){
                sql.SET("status=#{status}");
            }
            /*if(site.getIsExit()!=null && site.getIsExit().trim().length()!=0){
                sql.SET("is_exit=#{isExit}");
            }*/
            sql.WHERE("id=#{id}");
            return sql.toString();
        }
    }


}
